<template>
  <div ref="content" class="content">
    <div :class="{header:needTagsView && fixedHeader}">
      <h2>房屋管理模块</h2>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="tableData.filter(data => !search || data.houseResidentialQuarters.toLowerCase().includes(search.toLowerCase()))"
      tooltip-effect="dark"
      height="600px"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        prop="houseId"
        label="房屋编号"
        width="120"
      />
      <el-table-column
        prop="houseAddrass"
        label="房屋所在区域"
      />
      <el-table-column
        prop="houseResidentialQuarters"
        label="房屋所在小区"
        show-overflow-tooltip
      />
      <el-table-column
        prop="houseNumber"
        label="房屋门牌号"
        show-overflow-tooltip
      />
      <el-table-column
        fixed="right"
      >
        <template slot="header">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入小区名进行检索"
          />
        </template>
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="查看此房屋的数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="lookUp(scope.$index, tableData)"
            >
              查看
            </el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="修改此房屋的数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="update(scope.$index, tableData)"
            >
              修改
            </el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除此房屋的数据" placement="bottom-end">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="deleteRow(scope.$index, tableData)"
            >
              删除
            </el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align:center"
      background
      :page-sizes="[20, 40, 80, 100, 200]"
      :page-size="20"
      layout="sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- dialog 查看房屋信息 -->
    <el-dialog title="房屋信息" :visible.sync="dialogVisible" :before-close="houseformClose">
      <el-form :model="houseform">
        <el-form-item label="房屋编号" :label-width="formLabelWidth">
          <el-input v-model="houseform.houseId" autocomplete="off" readonly />
        </el-form-item>
        <el-form-item label="房屋所在区域" :label-width="formLabelWidth">
          <el-input v-model="houseform.houseAddrass" autocomplete="off" :readonly="!houseformState" />
        </el-form-item>
        <el-form-item label="房屋所在小区" :label-width="formLabelWidth">
          <el-input v-model="houseform.houseResidentialQuarters" autocomplete="off" :readonly="!houseformState" />
        </el-form-item>
        <el-form-item label="房屋门牌号" :label-width="formLabelWidth">
          <el-input v-model="houseform.houseNumber" autocomplete="off" :readonly="!houseformState" />
        </el-form-item>
      </el-form>
      <div v-if="houseformState" slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getAllHouse, deleteHouse, updateHouse } from '@/api/house'
export default {
  components: {},
  data() {
    return {
      loading: false,
      total: 0,
      pageSize: 20,
      pageCurrent: 1,
      tableData: [],
      multipleSelection: [],
      dialogVisible: false,
      formLabelWidth: '120px',
      houseform: {},
      houseformState: false,
      search: ''
    }
  },
  computed: {
    ...mapState({
      needTagsView: state => state.settings.tagsView,
      fixedHeader: state => state.settings.fixedHeader
    })
  },
  created() {
    this.init()
  },
  methods: {
    // 初始化函数
    init() {
      this.loading = true
      getAllHouse(this.pageSize, this.pageCurrent).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    },
    // 左侧选择框
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 删除行
    deleteRow(index, rows) {
      this.deleteConfirm(index, rows)
    },
    // 查看信息
    lookUp(index, rows) {
      this.houseform = JSON.parse(JSON.stringify(rows[index]))
      this.dialogVisible = true
    },
    // 修改信息
    update(index, rows) {
      this.houseform = JSON.parse(JSON.stringify(rows[index]))
      this.houseformState = true
      this.dialogVisible = true
    },
    // 弹窗表单关闭回调函数
    houseformClose() {
      this.houseformState = false
      this.dialogVisible = false
    },
    //  更新二次确认弹窗
    updateConfirm() {
      this.$confirm('确定要更新数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateHouse(this.houseform).then(rsp => {
          if (rsp.result) {
            this.$message({
              type: 'success',
              message: '更新成功'
            })
          } else {
            this.$notify({
              title: '操作失败',
              message: '更新房屋数据失败，请联系管理员',
              type: 'warning'
            })
          }
          this.houseformState = false
          this.dialogVisible = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
        this.houseformState = false
      })
    },
    //  删除二次确认弹窗
    deleteConfirm(index, rows) {
      this.$confirm('确定要删除此房屋数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteHouse(rows[index].houseId).then(rsp => {
          if (rsp.result) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.houseformState = false
            this.dialogVisible = false
            rows.splice(index, 1)
          } else {
            this.$notify({
              title: '操作失败',
              message: '删除房屋失败，请联系管理员',
              type: 'warning'
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
        this.houseformState = false
      })
    },

    // 改变分页数量
    handleSizeChange(val) {
      this.loading = true
      this.pageCurrent = 1
      getAllHouse(val, 1).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    },
    // 换页
    handleCurrentChange(val) {
      this.loading = true
      getAllHouse(this.pageSize, val).then(resp => {
        this.total = resp.obj.total
        this.tableData = resp.obj.list
        this.loading = false
      })
    }
  }
}
</script>

<style>
 .content {
   margin: 20px;
 }
 .header {
   margin-top: 54px;
 }
</style>
